package com.vitabuybuy.tv.ui.shopcenter

import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import cc.shinichi.library.ImagePreview
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.net.loadingtype.LoadingType
import com.library.common.widget.RecycleViewDivider
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityCommodityCommentBinding
import com.vitabuybuy.tv.dto.OrderDetailDto
import com.vitabuybuy.tv.ui.shopcenter.adapter.CommentListAdapter
import com.vitabuybuy.tv.ui.shopcenter.viewmodel.GoodsDetailsViewModel
import com.vitabuybuy.tv.ui.tencent.superplay.SuperPlayVidelMainActivity
import kotlinx.android.synthetic.main.activity_commodity_comment.*

class CommodityCommentActivity :
    BaseActivity<GoodsDetailsViewModel, ActivityCommodityCommentBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_commodity_comment

    private val commentAdapter by lazy { CommentListAdapter(CommentListAdapter.DISPLAY_PIC_VIDEO) }
    private var goodId :Int?= -1
    private var pageNumber = 1
    private var isImage = false
    override fun onGetBundle(bundle: Bundle?) {
        goodId = bundle?.getInt("goodId")
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.comment_list))
        mBinding.viewModel = viewModel
        listview.apply {
            adapter = commentAdapter
            layoutManager = LinearLayoutManager(mContext)
            addItemDecoration(RecycleViewDivider(mContext, 1))
        }
    }

    override fun observeData() {
        viewModel.getGoodsCommentList(goodId, isImage, pageNumber, 10)
        viewModel.commentList.observe(this, Observer {
            if (pageNumber == 1) {
                commentAdapter.setNewData(it)
            } else {
                commentAdapter.addData(it)
            }
            if (commentAdapter.mData.isEmpty()) {
                viewModel.statusView.set(LoadingType.SHOW_EMPTY)
            }else{
                viewModel.statusView.set(LoadingType.SHOW_CONTENT)
            }
            refresh.onLoad(it.size)
        })
    }

    override fun eventHandling() {
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber++
                viewModel.getGoodsCommentList(goodId, isImage, pageNumber, 10)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                viewModel.getGoodsCommentList(goodId, isImage, pageNumber, 10)
            }
        })
        commentAdapter.onItemClick =
            { childPos: Int, images: ArrayList<OrderDetailDto.ItemListBean.VideoAndPicBean> ->
                if (images[childPos].tag == "video") {
                    SuperPlayVidelMainActivity.openActivity(mContext,images[childPos].fileUrl,
                        images[childPos].cover)
                } else {
                    val previewImages = ArrayList<String>()
                    for (bean in images) {
                        if (bean.tag == "image") {
                            previewImages.add(bean.fileUrl)
                        }
                    }
                    ImagePreview.getInstance().setContext(mContext)
                        .setIndex(0)
                        .setShowDownButton(false)
                        .setImageList(previewImages)
                        .setLoadStrategy(ImagePreview.LoadStrategy.NetworkAuto)
                        .setZoomTransitionDuration(300)
                        .setEnableDragClose(true)
                        .setErrorPlaceHolder(R.drawable.default_image).start()
                }
            }
        rgType.setOnCheckedChangeListener { _, checkedId ->
            isImage = when (checkedId) {
                R.id.rbMedia -> true
                else -> false
            }
            pageNumber = 1
            viewModel.getGoodsCommentList(goodId, isImage, pageNumber, 10)
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 -> {
                pageNumber = 1
                viewModel.getGoodsCommentList(goodId, isImage, pageNumber, 10)
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>, goodId: Int?) {
            mContext.startActivity(
                bundleOf(Pair("goodId", goodId)),
                CommodityCommentActivity::class.java
            )
        }
    }
}
